# Makefile for mp4_to_rtsp

# 编译器设置
CC ?= gcc
CROSS_COMPILE ?=

# 如果指定了交叉编译工具链前缀，则使用它
ifeq ($(strip $(CROSS_COMPILE)),)
	CC = gcc
else
	CC = $(CROSS_COMPILE)gcc
endif

# 编译选项
CFLAGS = -Wall -g

# 初始化FFmpeg相关变量
FFMPEG_CFLAGS = 
FFMPEG_LDFLAGS = 

# 检测FFmpeg状态的变量
FFMPEG_FOUND_METHOD = "unknown"

# 检查是否设置了自定义FFmpeg路径
ifdef FFMPEG_INCLUDE_PATH
	FFMPEG_CFLAGS += -I$(FFMPEG_INCLUDE_PATH)
	FFMPEG_LDFLAGS += -L$(FFMPEG_LIB_PATH)
	FFMPEG_FOUND_METHOD = "custom_path"
else
	# 尝试使用pkg-config获取FFmpeg库配置
	PKG_CONFIG_RESULT := $(shell pkg-config --exists libavformat libavcodec libavutil && echo "yes" || echo "no")
	ifeq ($(PKG_CONFIG_RESULT), yes)
		FFMPEG_CFLAGS += $(shell pkg-config --cflags libavformat libavcodec libavutil)
		FFMPEG_LDFLAGS += $(shell pkg-config --libs libavformat libavcodec libavutil)
		FFMPEG_FOUND_METHOD = "pkg_config"
	else
		# 尝试使用常见的系统路径
		COMMON_INCLUDE_PATHS = /usr/include /usr/local/include /opt/include
		COMMON_LIB_PATHS = /usr/lib /usr/local/lib /opt/lib
		
		# 检查是否存在常见的FFmpeg头文件
		HAVE_FFMPEG = $(shell for path in $(COMMON_INCLUDE_PATHS); do \
		  if [ -f "$$path/libavformat/avformat.h" ]; then \
		    echo "1"; \
		    export FFMPEG_INCLUDE_PATH_TMP=$$path; \
		    break; \
		  fi; \
		done)
		
		# 检查是否存在常见的FFmpeg库文件
		ifneq ($(strip $(HAVE_FFMPEG)),)
			FFMPEG_INCLUDE_PATH = $(shell echo $$FFMPEG_INCLUDE_PATH_TMP)
			FFMPEG_LIB_PATH = $(shell for path in $(COMMON_LIB_PATHS); do \
			  if [ -f "$$path/libavformat.so" ]; then \
			    echo $$path; \
			    break; \
			  fi; \
			done)
			
			FFMPEG_CFLAGS += -I$(FFMPEG_INCLUDE_PATH)
			FFMPEG_LDFLAGS += -L$(FFMPEG_LIB_PATH)
			FFMPEG_FOUND_METHOD = "system_path"
		else
			FFMPEG_FOUND_METHOD = "not_found"
		endif
	endif
endif

# FFmpeg库链接
FFMPEG_LIBS = -lavformat -lavcodec -lavutil

# 目标文件
TARGET = mp4_to_rtsp
SRCS = mp4_to_rtsp.c
OBJS = $(SRCS:.c=.o)

# 编译规则
all: show_config check_ffmpeg $(TARGET)

# 显示FFmpeg配置信息
show_config:
	@echo "正在准备编译mp4_to_rtsp..."
	@if [ "$(FFMPEG_FOUND_METHOD)" = "custom_path" ]; then \
	  echo "使用自定义FFmpeg路径:"; \
	  echo "  头文件路径: $(FFMPEG_INCLUDE_PATH)"; \
	  echo "  库文件路径: $(FFMPEG_LIB_PATH)"; \
	elif [ "$(FFMPEG_FOUND_METHOD)" = "pkg_config" ]; then \
	  echo "使用pkg-config找到的FFmpeg库"; \
	elif [ "$(FFMPEG_FOUND_METHOD)" = "system_path" ]; then \
	  echo "在系统路径中找到FFmpeg库:"; \
	  echo "  头文件路径: $(FFMPEG_INCLUDE_PATH)"; \
	  echo "  库文件路径: $(FFMPEG_LIB_PATH)"; \
	elif [ "$(FFMPEG_FOUND_METHOD)" = "not_found" ]; then \
	  echo "警告: 未找到FFmpeg库!将在check_ffmpeg步骤中验证是否可以编译。"; \
	  echo "如果编译失败，请安装FFmpeg开发库或设置以下环境变量:"; \
	  echo "  export FFMPEG_INCLUDE_PATH=/path/to/ffmpeg/include"; \
	  echo "  export FFMPEG_LIB_PATH=/path/to/ffmpeg/lib"; \
	fi

# 检查FFmpeg库是否可用
check_ffmpeg:
	@echo "正在检查FFmpeg库..."
	@# 尝试编译一个简单的测试程序来验证FFmpeg库是否可用
	@echo "#include <libavformat/avformat.h>\nint main() { return 0; }" > .ffmpeg_test.c
	@$(CC) $(CFLAGS) $(FFMPEG_CFLAGS) -o .ffmpeg_test .ffmpeg_test.c $(FFMPEG_LIBS) $(FFMPEG_LDFLAGS) >/dev/null 2>&1 || (\
	  echo "错误: 无法编译FFmpeg测试程序!"; \
	  echo "请检查FFmpeg库是否正确安装，或者设置以下环境变量: "; \
	  echo "  export FFMPEG_INCLUDE_PATH=/path/to/ffmpeg/include"; \
	  echo "  export FFMPEG_LIB_PATH=/path/to/ffmpeg/lib"; \
	  echo "然后重新运行make命令。"; \
	  rm -f .ffmpeg_test.c; \
	  exit 1;)
	@rm -f .ffmpeg_test .ffmpeg_test.c
	@echo "FFmpeg库检查通过!"

$(TARGET): $(OBJS)
	$(CC) $(CFLAGS) -o $@ $^ $(FFMPEG_LIBS) $(FFMPEG_LDFLAGS)

%.o: %.c
	$(CC) $(CFLAGS) $(FFMPEG_CFLAGS) -c -o $@ $<

# 清理规则
clean:
	rm -f $(OBJS) $(TARGET) .ffmpeg_test .ffmpeg_test.c

# 显示帮助信息
tips:
	@echo "编译帮助:"
	@echo "  1. 使用系统FFmpeg库编译: make"
	@echo "  2. 指定FFmpeg库路径: export FFMPEG_INCLUDE_PATH=/path/to/ffmpeg/include && export FFMPEG_LIB_PATH=/path/to/ffmpeg/lib && make"
	@echo "  3. 交叉编译: make CROSS_COMPILE=arm-linux-gnueabihf-"
	@echo "  4. 检查FFmpeg安装: make check_ffmpeg"
	@echo "  5. 显示当前配置: make show_config"
	@echo "  6. 显示详细诊断信息: make diagnose"

# 诊断目标，显示当前的编译配置
diagnose:
	@echo "当前编译配置:"
	@echo "  CC = $(CC)"
	@echo "  CFLAGS = $(CFLAGS)"
	@echo "  FFMPEG_CFLAGS = $(FFMPEG_CFLAGS)"
	@echo "  FFMPEG_LDFLAGS = $(FFMPEG_LDFLAGS)"
	@echo "  FFMPEG_LIBS = $(FFMPEG_LIBS)"
	@echo "  FFMPEG_INCLUDE_PATH = $(FFMPEG_INCLUDE_PATH)"
	@echo "  FFMPEG_LIB_PATH = $(FFMPEG_LIB_PATH)"
	@echo "  FFMPEG_FOUND_METHOD = $(FFMPEG_FOUND_METHOD)"
	@echo "\npkg-config状态:"
	@pkg-config --libs --cflags libavformat libavcodec libavutil 2>/dev/null || echo "pkg-config无法找到FFmpeg库"
	@echo "\n系统FFmpeg文件检查:"
	@for path in /usr/include /usr/local/include /opt/include; do \
	  if [ -f "$$path/libavformat/avformat.h" ]; then \
	    echo "  找到头文件: $$path/libavformat/avformat.h"; \
	  fi; \
	done
	@for path in /usr/lib /usr/local/lib /opt/lib; do \
	  if [ -f "$$path/libavformat.so" ]; then \
	    echo "  找到库文件: $$path/libavformat.so"; \
	  fi; \
	done

.PHONY: all clean tips check_ffmpeg show_config diagnose